<?php
namespace App\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class TogetherGame extends BaseModel
{
    protected $connection = 'api';//数据库地址

    protected $table = 'together_game'; //一起玩游戏列表
    protected $togetherGameOpenConfig = 'together_game_config'; //一起玩功能开关
    protected $togetherGameSignConfig = 'together_game_sign_config';//一起玩签到配置表

    //列表
    public function getGameLsit($page, $limit, $searchId, $kwd, $status){
        $where = function($q)use($searchId,$kwd,$status) {  
            $q->where('is_deleted',0);//未删除数据
            if(!empty($searchId)){
                $q->where('id',$searchId);
            }
            //关键词
            if(!empty($kwd)){
                $q->where('game_name','like',$kwd.'%');
            }
            //状态值
            if(!empty($status)){
                $q->where('status',$status);
            }
        };
        $list = DB::connection($this->connection)->table($this->table)->where($where)->paginate($limit,['*'],'page',$page);
        $list = objToArray($list);
        return $list;
    }

    //一起玩数据添加修改操作
    public function dataChange($id, $gameName, $diamond_value, $jump_url, $video_url, $scale, $sort, $status, $defaultUrl, $iconUrl){
        $params = [
            'id' => $id,//数据id
            'game_name' => $gameName,//游戏名称
            'diamond_value' => $diamond_value,//钻石限制
            'jump_url' => $jump_url,//跳转链接
            'video_url' => $video_url,//视频链接
            'default_url' => $defaultUrl,//默认图
            'icon_url' => $iconUrl,//图标
            'scale' => $scale,//视频比例
            'sort' => $sort,//排序更改
            'status' => $status,//显示状态
        ];
        return curl_request(getKkaudioApiUrl() . '/inner/togethergame/insert_update', $params, 'urlencoded', 'POST');
    }

    //删除数据
    public function deleteGame($id){
        $params = [
            'id' => $id
        ];
        return curl_request(getKkaudioApiUrl() . '/inner/togethergame/deletegame', $params, 'urlencoded', 'POST');  
    }

    //一起玩开关控制列表
    public function gameOpenList(){
        $list = DB::connection($this->connection)->table($this->togetherGameOpenConfig)->first();
        return objToArray($list);
    }

    //设置开关
    public function setGameOpen($status, $diamond){
        $params = [
            'status' => $status,
            'diamond' => $diamond
        ];
        return curl_request(getKkaudioApiUrl() . '/inner/togethergame/setgameopen', $params, 'urlencoded', 'POST'); 
    }

    //一起玩-签到列表
    public function daySign($page,$limit){
        $list = DB::connection($this->connection)->table($this->togetherGameSignConfig)->where('status',1)->paginate($limit,['*'],'page',$page);
        $list = objToArray($list);
        if(!empty($list['data'])){
            foreach($list['data'] as &$v){
                $v['sigin_type'] = $v['sigin_day'];
                $v['sigin_day'] = '第'.numberToChinese($v['sigin_day']).'天';  
            }
        }
        return $list;
    }

    //一起玩-签到配置
    public function setDaySign($id, $signday, $img, $sendType, $sendId, $sendNum, $status){
        $params = [
            'id' => $id,
            'sigin_day' => $signday,
            'pic' => $img,
            'send_type' => $sendType,
            'send_num' => $sendNum,
            'send_id' => $sendId,
            'status' => $status,
        ];
        return curl_request(getKkaudioApiUrl() . '/inner/togethergame/setdaysign', $params, 'urlencoded', 'POST'); 
    }
}